package com.k2091.washcar.controller;


import com.k2091.washcar.entity.CarOrder;
import com.k2091.washcar.entity.FormInline;
import com.k2091.washcar.service.ICarOrderService;
import lombok.Lombok;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2021-06-01
 */
@RestController
@RequestMapping("/api/order")
public class CarOrderController {

    @Autowired
    private ICarOrderService orderService;

    //用户发起预约
    @RequestMapping("/addOrder")
    @ResponseBody
    public int addOrder(@RequestBody CarOrder carOrder) {
        int r = orderService.addOrder(carOrder);
        return r;
    }


    //洗车员接单 : 传入订单id(id) 洗车员id (cleanerId)
    //返回结果  非 0 为成功
    @RequestMapping("/receiveOrder")
    @ResponseBody
    public int receiveOrder(@RequestBody Map map) {
        int r = orderService.updateOrder(Long.valueOf(map.get("id").toString()),
                Long.valueOf(map.get("cleanerId").toString()));
        return r;
    }

    //洗车员完成订单  非 0 成功
    //传入参数 : 订单id(id), 洗车员id(cleanerId)
    @RequestMapping("/cleanerFinish")
    @ResponseBody
    public int cleanerFinish(@RequestBody Map map) {
        int r = orderService.updateOrderStatus(Long.valueOf(map.get("id").toString()), Long.valueOf(map.get("cleanerId").toString()));
        return r;
    }

    //用户确认订单完成
    //传入参数: 订单 id (id),评价等级(rank)
    @RequestMapping("/userFinish")
    @ResponseBody
    public int userFinish(@RequestBody Map map) {
        Object rankObj = map.get("rank");
        String rank = null;
        if(rankObj != null){
            rank = rankObj.toString();
        }
        return orderService.orderFinish(Long.valueOf(map.get("id").toString()), rank);
    }

    //用户评价
    //需传入参数:订单 id(id) ,评价等级(rank)
    @RequestMapping("userRank")
    @ResponseBody
    public int userRank(@RequestBody Map map) {
        int r = orderService.userRank(map.get("rank").toString(), Long.valueOf(map.get("id").toString()));
        return r;
    }

    //查询所有未接订单
    @RequestMapping("/selectOrderUnreceive")
    @ResponseBody
    public List<CarOrder> selectOrderUnreceive() {
        return orderService.selectUnreceiveOrder();
    }

    //查看某洗车员所有订单
    //传入参数: 洗车员id(cleanerId)
    @RequestMapping("/cleanerOrder")
    @ResponseBody
    public List<CarOrder> cleanerOrder(@RequestBody Map map) {
        return orderService.selectOrderByCleaner(Long.valueOf(map.get("cleanerId").toString())
        );
    }

    //用户查看本人所有未评价订单
    //传入参数:用户id (userId)
    @RequestMapping("/orderJudge")
    @ResponseBody
    public List<CarOrder> unjudge(@RequestBody Map map) {
        return orderService.userUnRank(Long.valueOf(map.get("userId").toString()));
    }

    //用户查看本人所有订单
    //传入参数: 用户id (userId)
    @RequestMapping("/userAllOrder")
    @ResponseBody
    public List<CarOrder> userAllOrder(@RequestBody Map map) {
        return orderService.selectAllOrder(Long.valueOf(map.get("userId").toString()));
    }
}
